<?php

require_once ROOT_APPLICATION_MODULES_TEMPLATE_FORMS.DS."Menu.php";
require_once ROOT_APPLICATION_MODULES_TEMPLATE_OBJECTS.DS."Menu.php";
require_once ROOT_APPLICATION_MODULES_TEMPLATE_MODELS.DS."Menu.php";

class IndexController extends Zend_Controller_Action
{
	
    public function init()
    {
        /* Initialize action controller here */
    }
    
    public function addAction()
    {   
    	
    	$form = new Application_Form_Menu();
    	
    	$form->submit->setLabel('Add');
        
       	$this->view->form = $form;
        
        if ($this->getRequest()->isPost()) 
        {
        	//Get data from form.
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData))   //If data is valid 
            {
            	//Get data one by one
                $name = $form->getValue('name');
                $description = $form->getValue('description');
                $user_id = (int)$form->getValue('user_id');
                $user_name = $form->getValue('user_name');
               	$created_date = $form->getValue('created_date');
                $language = $form->getValue('language');
                
                $menu = new Menu(null, $name, $description, $user_id, $user_name, $created_date, $language);
                
                $menus = new End_Models_Menu();
                
                $menus->_insert($menu);
                
                $this->_helper->redirector('index');
            } else 
            {
            	$form->populate($formData);
            }
        }
    }
    
    public function deleteAction()
    {	
    	if ($this->getRequest()->isPost()) 
    	{
            $del = $this->getRequest()->getPost('del'); 
    		if ($del == 'Yes') 
            {
                $menu_id = (int)$this->getRequest()->getPost('menu_id');
                $menus = new End_Models_Menu();
                $menus->_delete($menu_id);
            }
            $this->_helper->redirector('index');
        } else 
        {
            $menu_id = $this->_getParam('menu_id', 0);
            $menus = new End_Models_Menu();
            $this->view->menu = Utility_Converter::objectToArray($menus->_find($menu_id));
        }
    }
    
    public function editAction()
    {
    	$form = new Application_Form_Menu();
        $form->submit->setLabel('Save');
        $this->view->form = $form;
       
        if ($this->getRequest()->isPost()) 
        {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) 
            {
                $menu_id = (int)$form->getValue('menu_id');
                $name = $form->getValue('name');
                $description = $form->getValue('description');
                $user_id = (int)$form->getValue('user_id');
                $user_name = $form->getValue('user_name');
                $created_date = $form->getValue('created_date');
                $language = $form->getValue('language');				
				$menu = new Menu($menu_id, $name, $description, $user_id, $user_name, $created_date, $language);
                $menus = new End_Models_Menu();						
                $menus->_update($menu);								
                $this->_helper->redirector('index');				
            } else 
            {
                $form->populate($formData);
            }
        } else 
        {
            $menu_id = $this->_getParam('menu_id', 0);
            if ($menu_id > 0) 
            {
                $menus = new End_Models_Menu();
                $form->populate(Utility_Converter::objectToArray($menu->_find($menu_id)));
            }
        }
    }
    
    public function indexAction()
    {
    	$modelMenu = new End_Models_Menu();
		$menu = $modelMenu->fetchAll();
		$this->view->Menu = $menu;
    }
    
}







